package interfaces.spawnMenu;

import interfaces.spawnMenu.costumize.CustomizeWindow;
import interfaces.spawnMenu.spawnMap.SpawnMap;
import interfaces.spawnMenu.squads.SquadsWindow;
import interfaces.superWidgets.InterfaceElement;

import java.util.ArrayList;
import java.util.List;

import gameStates.absGamesStates.gui.AbsSpawnState;
import logic.common.game.Game;
import logic.common.player.Player;
import logic.common.team.Team;
import logic.ships.hunter.Hunter;
import map.spawnStation.SpawnPosition;

public class SpawnMenu extends InterfaceElement implements AbsSpawnMenu {
	
	private SpawnMenuWindow currentWindow;
	private SelectWindow selectWindow;
	private CustomizeWindow customWindow;
	private SquadsWindow squadWindow;
	private SpawnMap spawnMap;
	private MoneyWindow moneyWindow;
	
	private Hunter hunter;
	private Team selectedTeam, enemyTeam;
	private List<SpawnMenuWindow> spawnWindows;
	
	private Game game;
	private boolean ready, customWindowEnabled;
	
	public SpawnMenu(AbsSpawnState spawnState, Game game) {
		super(spawnState);
		this.game = game;
				
		Player player = game.getCurrentPlayer();
		selectedTeam = player.getTeam();
		enemyTeam = player.getEnemyTeam();
		
		selectWindow = new SelectWindow(this);
		
		moneyWindow = new MoneyWindow(this);
		
		spawnWindows = new ArrayList<SpawnMenuWindow>();
		
		customWindow = new CustomizeWindow(selectedTeam, enemyTeam, this);
		spawnWindows.add(customWindow);
		
		squadWindow = new SquadsWindow(selectedTeam, enemyTeam, this);
		spawnWindows.add(squadWindow);
		
		spawnMap = new SpawnMap(game, selectedTeam, enemyTeam, this);
		spawnWindows.add(spawnMap);
		
		customWindowEnabled = true;
		
		init();
	}
	
	@Override
	public boolean canSwitchTeam() {
		return game.canSwitchTeam(game.getCurrentPlayer());
	}
	
	public Team getSelectedTeam() { return selectedTeam; }
	
	@Override
	public void changeSelectedTeam(Team newTeam) {
		enemyTeam = selectedTeam;
		selectedTeam = newTeam;
		hunter = null;
		
		for(SpawnMenuWindow window : spawnWindows) {
			window.changeTeam(newTeam);
		}
		selectWindow.teamChanged();
	}
	
	@Override
	public void changeSelectedHunter(Hunter hunter) {
		this.hunter = hunter;
		for(SpawnMenuWindow window : spawnWindows) {
			if(window != customWindow) window.changeHunter(hunter);
		}
		ready = false;
	}
	
	@Override
	public SpawnPosition getSelectedSpawnLocation() {
		return spawnMap.getSelectedSpawnLocation();
	}
	
	@Override
	public Hunter getSelectedHunter() { return hunter; }
	
	@Override
	public void setCurrentWindow(SpawnMenuWindow window) {
		if(!customWindowEnabled && game.getCurrentPlayer().isAlive() && window == customWindow) return;
		
		if(currentWindow != null && currentWindow.getParent() != null) 
			removeWidget(currentWindow);
		currentWindow = window;
		addWidget(currentWindow);
	}
	
	@Override
	public SelectWindow getSelectWindow() { return selectWindow; }
	
	@Override
	public boolean isReady() { return ready; }
	
	@Override
	public void setReady(boolean ready) {
		this.ready = ready;
		game.getCurrentPlayer().setSpawnable(ready);
	}
	
	@Override
	public SpawnMenuWindow getCurrentWindow() { return currentWindow; }
	
	@Override
	public CustomizeWindow getCustomWindow() { return customWindow; }
	
	@Override
	public SquadsWindow getTacticWindow() { return squadWindow; }
	
	@Override
	public SpawnMap getSpawnMap() { return spawnMap; }
	
	@Override
	public Game getGame() { return game; }
	
	@Override
	public void enableCustomizeWindow(boolean enable) {
		if(customWindowEnabled == enable) return;
		customWindowEnabled = enable;
		if(!customWindowEnabled && game.getCurrentPlayer().isAlive() && customWindow == currentWindow) 
			removeWidget(currentWindow);
	}
	
	public void init() {
		addWidget(selectWindow);
		addWidget(moneyWindow);
		
		setCurrentWindow(customWindow);
	}
	
	@Override
	public void updateMoney(int newMoney) {
		customWindow.updateMoney(newMoney);
		moneyWindow.updateMoney(newMoney);
	}
	
	@Override
	public void restore() {
		customWindow.restore();
	}
}